<template>
	<view class="content">
		<view class="title">
			<text class="iconfont icon-shop"></text>
			<text>优购生活馆</text>
		</view>
		<!-- 商品列表 -->
		<view class="goods-list">
			<view class="goods-item" v-for="(item, index) in goodsList" :key="index">
				<text class="iconfont" :class="item.checked?'icon-checked':'icon-unchecked'" @click="isChecked(item)"></text>
				<image :src="item.goods_small_logo" alt="" />
				<view class="right">
					<text class="text-line2">{{item.goods_name}}</text>
					<view class="btm">
						<text class="price">￥<text>{{item.goods_price}}</text>.00</text>
						<view class="goods-num">
							<button @click="subtract(item)">-</button>
							<text>{{item.num}}</text>
							<button @click="add(item)">+</button>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="account">
			<view class="select-all" @click="toggleAll">
				<text class="iconfont" :class="isAll?'icon-checked':'icon-unchecked'"></text>
				<text>全选</text>
				<view class="price-wrapper">
					<view class="price">
						<view>合计:<text class="num">￥{{totalPrice}}</text></view>
						<text class="info">包含运费</text>
					</view>
					<view class="account-btn">结算({{totalNum}})</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				goodsList: []
			}
		},
		onShow() {
			console.log('显示')
			this.queryGoodsList()
			wx.setTabBarBadge({
				index: 2,
				text: this.$store.getters.getCartLen + ''
			})
		},
		onHide() {
			let cart = this.goodsList.map(item => {
				return {
					goodsId: item.goodsId,
					num: item.num,
					checked: item.checked
				}
			})
			uni.setStorageSync('cart', cart)
		},
		computed: {
			isAll: {
				get() {
					return this.goodsList.every(item => item.checked)
				},
				set(status) {
					// console.log(status)
					// 让每一项状态与当前状态一致
					this.goodsList.forEach(item => {
						item.checked = status
					})
				}
			},
			totalNum() {
				return this.goodsList.reduce((sum, item) => {
					return sum + (item.checked ? item.num : 0)
				}, 0)
			},
			totalPrice() {
				return this.goodsList.reduce((sum, item) => {
					return sum + (item.checked ? item.num * item.goods_price : 0)
				}, 0)
			}
		},
		methods: {
			async queryGoodsList() {
				let cart = this.$store.state.cart

				if (!cart.length) {
					return
				}
				console.log('仓库数据', cart)
				let idArry = cart.map(item => {
					return item.goodsId
				})
				let idStr = idArry.join(',')

				let _goodsList = await this.$request({
					url: '/api/public/v1/goods/goodslist?goods_ids=' + idStr
				})

				console.log('购物车数据', _goodsList)

				// goodsList与cart 合并数据
				this.goodsList = cart.map(item => {
					// 从goodsList 找到同样id 的对象
					let goods = _goodsList.find(v => {
						return v.goods_id === item.goodsId
					})
					return { ...item,
						...goods
					}
				})
				console.log('获取的数据', this.goodsList)
			},
			// 选中的订单
			isChecked(item) {
				item.checked = !item.checked
			},
			// 全选
			toggleAll() {
				this.isAll = !this.isAll
			},
			// 减少
			subtract(item) {
				if (item.num > 1) {
					item.num--;
				}
			},
			// 增加
			add(item) {
				if (item.num < 99) {
					item.num++;
				}
			}
		}
	}
</script>

<style lang="less">
	.iconfont {
		font-size: 44rpx;
	}

	.title {
		height: 88rpx;
		display: flex;
		align-items: center;

		.iconfont {
			color: #999;
			margin: 6rpx 12rpx 0 32rpx;
		}
	}

	.goods-item {
		height: 206rpx;
		background-color: #fff;
		border-top: 1rpx solid #ddd;
		display: flex;
		box-sizing: border-box;
		align-items: center;

		.iconfont {
			color: #eb4450;
			margin: 0 30rpx;
		}

		image {
			width: 160rpx;
			height: 160rpx;
		}

		.right {
			flex: 1;
			display: flex;
			flex-direction: column;
			margin: 0 20rpx 0 18rpx;

			.text-line2 {
				display: -webkit-box;
				-webkit-box-orient: vertical;
				-webkit-line-clamp: 3;
				overflow: hidden;
			}

			.btm {
				display: flex;
				margin-top: 20rpx;
				justify-content: space-between;

				.price {
					color: #eb4450;
					display: flex;

					>text {
						font-size: 24px;
					}
				}

				.goods-num {
					display: flex;
					align-items: center;

					button {
						width: 60rpx;
						height: 50rpx;
						box-sizing: border-box;
						display: flex;
						justify-content: center;
						align-items: center;
						// border: 4rpx solid #666;
					}

					text {
						margin: 0 30rpx;
					}
				}
			}
		}
	}

	.goods-list {
		position: absolute;
		bottom: 98rpx;
		top: 88rpx;
		width: 100%;
		overflow: auto;
		padding-bottom: 60rpx;
		background-color: #f4f4f4;
	}

	.account {
		height: 98rpx;
		border-bottom: 1rpx solid #ddd;
		display: flex;
		position: absolute;
		left: 0;
		right: 0;
		bottom: 0;
		align-items: center;

		.iconfont {
			color: #eb4450;
			margin: 0 30rpx;
		}

		.account-btn {
			width: 230rpx;
			height: 98rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			color: #fff;
			background-color: #eb4450;
		}

		.select-all {
			flex: 1;
			display: flex;
			align-items: center;

			.price-wrapper {
				display: flex;
				flex: 1;
				justify-content: flex-end;
				align-items: center;
			}
		}

		.price {
			margin-right: 20rpx;

			.num {
				color: #eb4450;
			}

			.info {
				color: #999;
			}
		}
	}
</style>
